home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-09-11 | 5.7 KB | 217 lines | [TEXT/R*ch] |
- /*
-
- Listing 4: ToolPalette .java
-
- ToolPalette.java
- Define and create our tool palette.
-
- */
-
- // Import the generic Java classes.
- import java.applet.*;
- import java.awt.*;
- import java.io.*;
- import java.lang.*;
-
- // Our class definition.
- public class ToolPalette extends Frame
- {
- static int maxItems = 9;
- int theItemColorArray[] = new int[ maxItems ];
- Button bQuit, bAddRect, bAddButton, bDelete;
- Choice colorMenu = new Choice();
- Choice itemMenu = new Choice();
- Demo myDemo;
- Frame theToolPalette;
- Label thePanelLabel = new Label();
- Label theColorLabel = new Label( "Color:" );
- Label theItemLabel = new Label( "Item:" );
- MenuBar theButtonMenuBar;
- Panel pButtonPalette;
- Panel pColorPalette;
- Panel pBlankPanel;
-
-
- // The constructor for our class.
- public ToolPalette( Demo thisDemo )
- {
- // Variables we need for initialization.
- int defaultX = 20, defaultY = 100;
- int defaultWidth = 300, defaultHeight = 170;
- int i = 0;
- Rectangle theBounds = new Rectangle();
- String theStringBlack = new String( "Black" );
- String theStringGreen = new String( "Green" );
- String theStringPink = new String( "Pink" );
- String theStringBlue = new String( "Blue" );
- String theStringLightGray = new String(
- "Light Gray" );
- String theStringRed = new String( "Red" );
- String theStringCyan = new String( "Cyan" );
- String theStringMagenta = new String( "Magenta" );
- String theStringWhite = new String( "White" );
- String theStringDarkGray = new String( "Dark Gray" );
- String theStringOrange = new String( "Orange" );
- String theStringYellow = new String( "Yellow" );
- String theStringGray = new String( "Gray" );
- String theStringAppWindow = new String(
- "Applet window" );
- String theStringRectBack = new String(
- "Panel background" );
- String theStringRectText = new String(
- "Panel label" );
- String theStringButBack = new String(
- "Button background" );
- String theStringButText = new String(
- "Button label" );
-
- myDemo = thisDemo;
-
- pButtonPalette = new Panel();
- pButtonPalette.setLayout( new GridLayout( 3, 3 ) );
-
- bQuit = new Button();
- bAddRect = new Button();
- bAddButton = new Button();
- bDelete = new Button();
-
- bQuit.setLabel( "Quit" );
- bAddRect.setLabel( "Add Panel" );
- bAddButton.setLabel( "Add Button..." );
- bDelete.setLabel( "Delete Item" );
-
- pButtonPalette.add( bAddRect );
- pButtonPalette.add( bAddButton );
- pButtonPalette.add( bDelete );
- pButtonPalette.add( bQuit );
- pButtonPalette.add( new Label() );
- pButtonPalette.add( new Label() );
- pButtonPalette.add( new Label() );
-
- theItemColorArray[ 0 ] = 12;
-
- for ( i = 1; i < maxItems; i++ )
- {
- theItemColorArray[ i ] = i + 1;
- }
-
- colorMenu.addItem( theStringBlack );
- colorMenu.addItem( theStringGreen );
- colorMenu.addItem( theStringPink );
- colorMenu.addItem( theStringBlue );
- colorMenu.addItem( theStringLightGray );
- colorMenu.addItem( theStringRed );
- colorMenu.addItem( theStringCyan );
- colorMenu.addItem( theStringMagenta );
- colorMenu.addItem( theStringWhite );
- colorMenu.addItem( theStringDarkGray );
- colorMenu.addItem( theStringOrange );
- colorMenu.addItem( theStringYellow );
- colorMenu.addItem( theStringGray );
-
- itemMenu.addItem( theStringAppWindow );
- itemMenu.addItem( theStringRectBack );
- itemMenu.addItem( theStringRectText );
- itemMenu.addItem( theStringButBack );
- itemMenu.addItem( theStringButText );
-
- pColorPalette = new Panel();
- pColorPalette.setLayout( new GridLayout( 4, 2 ) );
-
- pColorPalette.add( theColorLabel );
- pColorPalette.add( colorMenu );
- pColorPalette.add( new Label() );
- pColorPalette.add( new Label() );
- pColorPalette.add( theItemLabel );
- pColorPalette.add( itemMenu );
- pColorPalette.add( new Label() );
- pColorPalette.add( new Label() );
-
- itemMenu.select( 0 );
- colorMenu.select( theItemColorArray[ 0 ] );
-
- pBlankPanel = new Panel();
- pBlankPanel.setLayout( new FlowLayout() );
- theBounds = pBlankPanel.bounds();
- pBlankPanel.reshape( theBounds.x, theBounds.y, 20, 20 );
-
- setBackground( Color.lightGray );
- setMenuBar( theButtonMenuBar );
- setResizable( true );
- setTitle( "Tool Palette" );
- reshape( defaultX, defaultY, defaultWidth,
- defaultHeight );
- add( "North", pButtonPalette );
- add( "Center", pColorPalette );
-
- show();
- }
-
- // Override the action() method of our parent class.
- public boolean action( Event evt, Object arg )
- {
- boolean eventHandled = false;
- int theColor = 0, theItem = 0;
- Object theTarget;
-
- if ( evt.target instanceof Choice )
- {
- theTarget = ( Choice )evt.target;
-
- if ( theTarget == itemMenu )
- {
- theItem =
- ( ( Choice )evt.target ).getSelectedIndex();
- theColor = theItemColorArray[ theItem ];
- colorMenu.select( theColor );
-
- eventHandled = true;
- }
-
- if ( theTarget == colorMenu )
- {
- theColor =
- ( ( Choice )evt.target ).getSelectedIndex();
- theItem = itemMenu.getSelectedIndex();
- theItemColorArray[ theItem ] = theColor;
-
- if ( theItem == 0 )
- eventHandled = myDemo.doSetBackColor( theColor );
- else
- eventHandled =
- myDemo.doChangeColor( theItem, theColor );
-
- eventHandled = true;
- }
- }
- else if ( evt.target instanceof Button )
- {
- theTarget = ( Button )evt.target;
-
- if ( theTarget == bQuit )
- myDemo.doDestroy();
-
- if ( theTarget == bAddRect )
- eventHandled = myDemo.doAddRect();
-
- if ( theTarget == bAddButton )
- eventHandled = myDemo.doCreateButtonFrame();
-
- if ( theTarget == bDelete )
- eventHandled = myDemo.doDeleteItem();
- }
-
- if ( eventHandled )
- return true;
- else
- return false;
- }
-
- // Return an int representing the color of the requested item.
- public int doGetColor( int itemNo )
- {
- return theItemColorArray[ itemNo ];
- }
-
- }
-